package com.atguigu.cloud.controller;

import cn.hutool.core.date.DateUtil;
import com.atguigu.cloud.apis.PayFeignApi;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.rest.ResultData;
import com.atguigu.cloud.rest.ReturnCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author HanYiMing
 * @description
 * @date 2024/3/20
 * @lastEditDate 2024/3/20
 * @lastEditAuthor
 * @lastDescription
 */
@RestController
public class OrderController {

    @Autowired
    private PayFeignApi payFeignApi;

    @PostMapping("/feign/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO) {
        return payFeignApi.addPay(payDTO);
    }

    @GetMapping("/feign/pay/getById/{id}")
    public ResultData getPayInfo(@PathVariable(value = "id") Integer id) {
        ResultData byId = null;
        try {
            System.out.println("调用开始----：" + DateUtil.now());
            byId = payFeignApi.getById(id);

        } catch (Exception e) {
            System.out.println("调用结束----：" + DateUtil.now());
            e.printStackTrace();
            return ResultData.fail(ReturnCodeEnum.RC500.getCode(), "请求失败");
        }
        return byId;
    }

    //    @GetMapping("/consumer/pay/getAll")
//    public ResultData getOrderAll() {
//        return rpayFeignApi.get;
//    }
//
//    @GetMapping("/consumer/pay/update")
//    public ResultData updateOrder(PayDTO payDTO) {
//        return restTemplate.postForObject(PaymentSrv_URL + "/pay/update", payDTO, ResultData.class);
//    }
//
//    @GetMapping("/consumer/pay/del/{id}")
//    public ResultData delOrder(@PathVariable(value = "id") Integer id) {
//        return restTemplate.getForObject(PaymentSrv_URL + "/pay/del/" + id, ResultData.class);
//    }

    @GetMapping("/feign/pay/mylb")
    public String getInfoByConsul() {
        return payFeignApi.mylb();
    }


//    @Resource
//    private DiscoveryClient discoveryClient;
//
//    @GetMapping("/consumer/discovery")
//    public String discovery() {
//        List<String> services = discoveryClient.getServices();
//        for (String service : services) {
//            System.out.println("service = " + service);
//        }
//
//        System.out.println("=============================");
//        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
//        for (ServiceInstance instance : instances) {
//            System.out.println(instance.getServiceId() + "\t" + instance.getPort() + "\t" + instance.getUri());
//        }
//
//        return instances.get(0).getServiceId() + ":" + instances.get(0).getPort();
//    }
}
